package com.melesta.payment;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import com.melesta.engine.Error;
import com.melesta.engine.Log;
import com.melesta.payment.interfaces.IActivityStateListener;
import com.melesta.payment.interfaces.IRequest;
import com.melesta.payment.interfaces.IRequestBuilder;
import com.melesta.payment.interfaces.IRequestFilter;
import com.melesta.payment.interfaces.IResponse;
import com.melesta.payment.interfaces.ISystem;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class PaymentManager
  implements IActivityStateListener
{
  private static PaymentManager inst;
  private final List<IRequest> activeRequests = new ArrayList();
  private Handler handler;
  private final Queue<IRequest> requests = new LinkedList();
  private final Map<String, IRequest> requestsList = new HashMap();
  private final Queue<IResponse> responses = new LinkedList();
  private final Map<String, ISystem> systemsList = new HashMap();

  private void addRequest(IRequest paramIRequest)
  {
    this.requestsList.put(paramIRequest.getName(), paramIRequest);
  }

  public static PaymentManager getInstance()
  {
    if (inst == null)
      inst = new PaymentManager();
    return inst;
  }

  private void loadPayments(Node paramNode)
  {
    NodeList localNodeList = paramNode.getChildNodes();
    int i = 0;
    if (i < localNodeList.getLength())
    {
      Node localNode1 = localNodeList.item(i);
      NamedNodeMap localNamedNodeMap = localNode1.getAttributes();
      if (localNamedNodeMap == null);
      while (true)
      {
        i++;
        break;
        Node localNode2 = localNamedNodeMap.getNamedItem("name");
        if (localNode2 != null)
        {
          String str1 = localNode2.getNodeValue();
          String str2 = localNode1.getNodeName();
          if ((str1 != null) && (str1.length() != 0) && (str2 != null) && (str2.length() != 0))
          {
            ISystem localISystem = getSystem(str2);
            if (localISystem != null)
            {
              IRequestBuilder localIRequestBuilder = localISystem.getRequestBuilder();
              if (localIRequestBuilder != null)
              {
                for (int j = 0; j < localNamedNodeMap.getLength(); j++)
                {
                  Node localNode3 = localNamedNodeMap.item(j);
                  localIRequestBuilder.set(localNode3.getNodeName(), localNode3.getNodeValue());
                }
                addRequest(localIRequestBuilder.createRequest());
              }
            }
          }
        }
      }
    }
  }

  private void loadSystems(Node paramNode)
  {
    NodeList localNodeList = paramNode.getChildNodes();
    int i = 0;
    if (i < localNodeList.getLength())
    {
      Node localNode1 = localNodeList.item(i);
      NamedNodeMap localNamedNodeMap = localNode1.getAttributes();
      if (localNamedNodeMap == null);
      while (true)
      {
        i++;
        break;
        String str = localNode1.getNodeName();
        if ((str != null) && (str.length() != 0))
        {
          ISystem localISystem = getSystem(str);
          if (localISystem != null)
            for (int j = 0; j < localNamedNodeMap.getLength(); j++)
            {
              Node localNode2 = localNamedNodeMap.item(j);
              localISystem.set(localNode2.getNodeName(), localNode2.getNodeValue());
            }
        }
      }
    }
  }

  private void postRequest(IRequest paramIRequest)
  {
    Log.d("payment", "Post request: " + paramIRequest);
    if (paramIRequest == null)
      return;
    synchronized (this.requests)
    {
      this.requests.add(paramIRequest);
      return;
    }
  }

  public static void tryBuy(String paramString, float paramFloat)
  {
    Log.d("payment", "Try buy: " + paramString);
    try
    {
      PaymentManager localPaymentManager = getInstance();
      localPaymentManager.postRequest(localPaymentManager.getRequest(paramString).setMultiplier(paramFloat));
      return;
    }
    catch (Exception localException)
    {
      Object[] arrayOfObject = new Object[2];
      arrayOfObject[0] = paramString;
      arrayOfObject[1] = Float.valueOf(paramFloat);
      Error.processException(String.format("tryBuy (%s, %f)", arrayOfObject), localException);
    }
  }

  public static void tryProcess()
  {
    getInstance().process();
  }

  public void addSystem(ISystem paramISystem)
  {
    this.systemsList.put(paramISystem.getName(), paramISystem);
  }

  public IRequest getActiveRequest(IRequestFilter paramIRequestFilter)
  {
    Iterator localIterator = this.activeRequests.iterator();
    while (localIterator.hasNext())
    {
      IRequest localIRequest = (IRequest)localIterator.next();
      if (paramIRequestFilter.accept(localIRequest))
        return localIRequest;
    }
    return null;
  }

  public List<IRequest> getAllRequests(String paramString)
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.requestsList.values().iterator();
    while (localIterator.hasNext())
    {
      IRequest localIRequest = (IRequest)localIterator.next();
      if (localIRequest.getPaymentSystemName().equals(paramString))
        localArrayList.add(localIRequest);
    }
    return localArrayList;
  }

  public IRequest getRequest(IRequestFilter paramIRequestFilter)
  {
    Iterator localIterator = this.requestsList.values().iterator();
    while (localIterator.hasNext())
    {
      IRequest localIRequest = (IRequest)localIterator.next();
      if (paramIRequestFilter.accept(localIRequest))
        return localIRequest;
    }
    return null;
  }

  public IRequest getRequest(String paramString)
  {
    return (IRequest)this.requestsList.get(paramString);
  }

  public String getRequestNameBySku(String paramString1, String paramString2)
  {
    Iterator localIterator = this.requestsList.values().iterator();
    while (localIterator.hasNext())
    {
      IRequest localIRequest = (IRequest)localIterator.next();
      if ((localIRequest.getSku().equals(paramString2)) && (localIRequest.getPaymentSystemName().equals(paramString1)))
        return localIRequest.getName();
    }
    return null;
  }

  public ISystem getSystem(String paramString)
  {
    return (ISystem)this.systemsList.get(paramString);
  }

  public void load(InputStream paramInputStream)
  {
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    while (true)
    {
      int i;
      try
      {
        NodeList localNodeList = localDocumentBuilderFactory.newDocumentBuilder().parse(paramInputStream).getDocumentElement().getChildNodes();
        i = 0;
        if (i < localNodeList.getLength())
        {
          Node localNode = localNodeList.item(i);
          if (localNode.getNodeName().equals("payments"))
            loadPayments(localNode);
          else if (localNode.getNodeName().equals("systems"))
            loadSystems(localNode);
        }
      }
      catch (Exception localException)
      {
        Error.processException(null, localException);
      }
      return;
      i++;
    }
  }

  public boolean onActivityResult(Context paramContext, int paramInt1, int paramInt2, Intent paramIntent)
  {
    boolean bool = false;
    Iterator localIterator = this.systemsList.values().iterator();
    while (localIterator.hasNext())
      bool |= ((ISystem)localIterator.next()).onActivityResult(paramContext, paramInt1, paramInt2, paramIntent);
    return bool;
  }

  public void onCreate(Context paramContext, Handler paramHandler)
  {
    this.handler = paramHandler;
    Iterator localIterator = this.systemsList.values().iterator();
    while (localIterator.hasNext())
      ((ISystem)localIterator.next()).onCreate(paramContext, paramHandler);
  }

  public void onDestroy(Context paramContext, Handler paramHandler)
  {
    Iterator localIterator = this.systemsList.values().iterator();
    while (localIterator.hasNext())
      ((ISystem)localIterator.next()).onDestroy(paramContext, paramHandler);
  }

  public void onPause(Context paramContext, Handler paramHandler)
  {
    Iterator localIterator = this.systemsList.values().iterator();
    while (localIterator.hasNext())
      ((ISystem)localIterator.next()).onPause(paramContext, paramHandler);
  }

  public void onResume(Context paramContext, Handler paramHandler)
  {
    Iterator localIterator = this.systemsList.values().iterator();
    while (localIterator.hasNext())
      ((ISystem)localIterator.next()).onResume(paramContext, paramHandler);
  }

  public void onStart(Context paramContext, Handler paramHandler)
  {
    Iterator localIterator = this.systemsList.values().iterator();
    while (localIterator.hasNext())
      ((ISystem)localIterator.next()).onStart(paramContext, paramHandler);
  }

  public void onStop(Context paramContext, Handler paramHandler)
  {
    Iterator localIterator = this.systemsList.values().iterator();
    while (localIterator.hasNext())
      ((ISystem)localIterator.next()).onStop(paramContext, paramHandler);
  }

  public void postResponse(IResponse paramIResponse)
  {
    if (paramIResponse == null)
      return;
    synchronized (this.responses)
    {
      this.responses.add(paramIResponse);
      return;
    }
  }

  public void process()
  {
    synchronized (this.requests)
    {
      IRequest localIRequest = (IRequest)this.requests.poll();
      if (localIRequest != null)
      {
        this.activeRequests.add(localIRequest);
        this.handler.post(localIRequest);
      }
    }
    synchronized (this.responses)
    {
      IResponse localIResponse = (IResponse)this.responses.poll();
      if (localIResponse != null)
        this.handler.post(localIResponse);
      return;
      localObject1 = finally;
      throw localObject1;
    }
  }

  public void purchaseProcessed(PaymentTransaction paramPaymentTransaction)
  {
    Iterator localIterator = this.systemsList.values().iterator();
    while (localIterator.hasNext())
    {
      ISystem localISystem = (ISystem)localIterator.next();
      if (localISystem.getName().equals(paramPaymentTransaction.paymentSystem))
        localISystem.purchaseProcessed(paramPaymentTransaction);
    }
  }

  public void removeActiveRequest(IRequest paramIRequest)
  {
    this.activeRequests.remove(paramIRequest);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.melesta.payment.PaymentManager
 * JD-Core Version:    0.6.2
 */